﻿
Partial Class WebForm_ManageRole_OpenManageRoleBrandMappingV2
    Inherits System.Web.UI.UserControl
#Region "事件"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindViewPoint()
        Else
            Me.mpSearch.Show()
        End If
    End Sub

    Protected Sub btQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btQuery.Click
        If Division <> "" Then
            Me.BindData(ddlViewPoint.SelectedValue)
            Me.btSubmit.Visible = True
        End If
    End Sub

    Protected Sub chkSelect_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim RowIndex As Integer = DirectCast(DirectCast(sender, CheckBox).NamingContainer, GridViewRow).RowIndex
        Dim ProdHierarchy As String = Me.gv.DataKeys(RowIndex).Item("Hierarchy").ToString()
        Dim ViewPoint As String = Me.gv.DataKeys(RowIndex).Item("ViewPoint").ToString()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        sql.Length = 0
        sql.AppendFormat("delete from RoleHierarchyMapping ")
        sql.AppendFormat("where RoleID='{0}' ", Me.RoleID)
        sql.AppendFormat("and ProdHierarchy='{0}' ", ProdHierarchy)
        sql.AppendFormat("and ViewPoint='{0}' ", ViewPoint)
        If DirectCast(sender, CheckBox).Checked = True Then
            sql.AppendFormat(";insert into RoleHierarchyMapping(RoleID,ProdHierarchy,ViewPoint) ")
            sql.AppendFormat("Select ")
            sql.AppendFormat("'{0}' ", Me.RoleID)
            sql.AppendFormat(",'{0}' ", ProdHierarchy)
            sql.AppendFormat(",'{0}' ", ViewPoint)
        End If

        dat.AccData(sql.ToString())
  
        Me.mpSearch.Show()
    End Sub

    Protected Sub gv_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv.PageIndexChanged
        Me.mpSearch.Show()
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

        End If
    End Sub
    Protected Sub btSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btSubmit.Click
        Me.InitDataAndShow()
        Me.BindGV()

    End Sub
#End Region

#Region "方法"

    Public Sub BindData(ByVal ViewPoint As String)
        Me.ProductHierarchyControl1.BindData(Division, ViewPoint)
    End Sub

    Public Sub InitDataAndShow()
        Me.mpSearch.Show()
        RoleID = Me.RoleID
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        sql.AppendFormat("Select * from Role ")
        sql.AppendFormat("where RoleID = '{0}' ", RoleID)
        Division = dat.GetData(sql).Rows(0)("Division").ToString()


    End Sub
    Protected Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select distinct  a.ViewPoint,a.LevelName,a.HierarchyText,a.HierarchyValue,Case isnull(rh.RoleID,'') when '' then '0' else '1' end CanAccess ")
        sql.AppendFormat(",case when a.LevelName ='Signature' then left(a.ProdHierarchy,2)+REPLICATE('X',16)  ")
        sql.AppendFormat("when a.LevelName ='Brand' then left(a.ProdHierarchy,4)+REPLICATE('X',14) ")
        sql.AppendFormat("when a.LevelName ='Sub-Brand' then left(a.ProdHierarchy,6)+REPLICATE('X',12) ")
        sql.AppendFormat("when a.LevelName ='Reference' then left(a.ProdHierarchy,8)+REPLICATE('X',10) ")
        sql.AppendFormat("when a.LevelName ='Nuance' then left(a.ProdHierarchy,10)+REPLICATE('X',8) ")
        sql.AppendFormat("when a.LevelName ='Axe' then REPLICATE('X',8)+a.HierarchyValue+REPLICATE('X',8) ")
        sql.AppendFormat("when a.LevelName ='Sub-Axe' then left(a.ProdHierarchy,14)+REPLICATE('X',4) ")
        sql.AppendFormat("when a.LevelName ='Class' then left(a.ProdHierarchy,16)+REPLICATE('X',2) ")
        sql.AppendFormat("when a.LevelName ='Function' then a.ProdHierarchy ")
        sql.AppendFormat("end Hierarchy ")
        sql.AppendFormat(" from ( ")
        For i As Integer = 0 To DataDT.Rows.Count - 1

            sql.AppendFormat("select distinct p.[{0}_Text] as HierarchyText,p.[{0}] as HierarchyValue ", DataDT.Rows(i)("HierarchyName"))
            sql.AppendFormat(",'{0}' as ViewPoint,'{1}' as LevelName ", ddlViewPoint.SelectedValue, DataDT.Rows(i)("HierarchyName"))
            sql.AppendFormat(",p.ProdHierarchy   ")
            sql.AppendFormat("from ProductHierarchy p ")
            sql.AppendFormat("where  p.ProdHierarchy in (select * from ProductHierarchy_All) ")
     
            If DataDT.Rows.Count > 1 And i <> DataDT.Rows.Count - 1 Then
                sql.AppendFormat("union all ")
            End If

        Next
        sql.AppendFormat(") a ")
      

        Dim FirstColumnName As String = ""
        Dim ColumnName As String = ""
        Dim NextColumnName As String = ""
        Dim ColumnValue As String = ""
        Dim NowLength As Integer = sql.Length
        Dim PrevColumnName As String = ""
        Dim PrevColumnValue As String = ""
        Dim Hierarchy As New HierarchyManager

        For j As Integer = 0 To Me.sqlArray.GetUpperBound(0)

            FirstColumnName = Me.sqlArray(0, 0).Replace("[", "").Replace("]", "")
            ColumnName = Me.sqlArray(j, 0).Replace("[", "").Replace("]", "")
            ColumnValue = Me.sqlArray(j, 1)

            If j + 1 <= Me.sqlArray.GetUpperBound(0) Then
                NextColumnName = Me.sqlArray(j + 1, 0).Replace("[", "").Replace("]", "")
            End If
            If j - 1 >= 0 Then
                PrevColumnName = Me.sqlArray(j - 1, 0).Replace("[", "").Replace("]", "")
                PrevColumnValue = Me.sqlArray(j - 1, 1)
            End If

            If ColumnValue <> "" And ColumnName <> "" Then
                If ColumnName = "Signature" OrElse ColumnName = "Brand" OrElse ColumnName = "Sub-Brand" OrElse ColumnName = "Reference" OrElse ColumnName = "Nuance" OrElse ColumnName = "Sub-Axe" OrElse ColumnName = "Class" OrElse ColumnName = "Function" Then

                    sql.Length = NowLength
                    sql.AppendFormat("left join RoleHierarchyMapping rh ")
                    sql.AppendFormat("on  left(a.ProdHierarchy," & Hierarchy.GetSequence(NextColumnName) & "*2)=left(rh.ProdHierarchy," & Hierarchy.GetSequence(NextColumnName) & "*2) ")
                    sql.AppendFormat("left join ProductHierarchy_All po on  a.ProdHierarchy=po.ProdHierarchy ")
                    sql.AppendFormat("where 1=1 ")
                    sql.AppendFormat("and a.LevelName= '{0}' ", NextColumnName)
                    sql.AppendFormat("and left(a.ProdHierarchy," & Hierarchy.GetSequence(ColumnName) & "*2) = '{0}' ", ColumnValue)

                ElseIf ColumnName = "Axe" Then

                    sql.Length = NowLength
                    sql.AppendFormat("left join RoleHierarchyMapping rh ")
                    sql.AppendFormat("on  left(a.ProdHierarchy," & Hierarchy.GetSequence(NextColumnName) & "*2)=left(rh.ProdHierarchy," & Hierarchy.GetSequence(NextColumnName) & "*2) ")
                    sql.AppendFormat("left join ProductHierarchy_All po on  a.ProdHierarchy=po.ProdHierarchy ")
                    sql.AppendFormat("where 1=1 ")
                    sql.AppendFormat("and a.LevelName= '{0}' ", NextColumnName)
                    sql.AppendFormat("and left(a.ProdHierarchy," & Hierarchy.GetSequence(PrevColumnName) & "*2) = '{0}' ", PrevColumnValue)

                End If
            ElseIf ColumnValue = "" And ColumnName = FirstColumnName Then

                sql.Length = NowLength
                sql.AppendFormat("left join RoleHierarchyMapping rh ")
                sql.AppendFormat("on  left(a.ProdHierarchy," & Hierarchy.GetSequence(ColumnName) & "*2)=left(rh.ProdHierarchy," & Hierarchy.GetSequence(ColumnName) & "*2) ")
                sql.AppendFormat("left join ProductHierarchy_All po on  a.ProdHierarchy=po.ProdHierarchy ")
                sql.AppendFormat("where 1=1 ")
                sql.AppendFormat("and a.LevelName= '{0}' ", ColumnName)
                If ColumnName = "Signature" Then
                    sql.AppendFormat("and a.HierarchyValue in (select Brand from DivisionBrandMappingView) ")
                End If
            End If

        Next
        sql.AppendFormat("order by a.HierarchyText ")

        Me.gv.DataSource = dat.GetData(sql.ToString())
        Me.gv.DataBind()
    End Sub
#End Region

#Region "繫結資料"

    Protected Sub BindViewPoint()
        ddlViewPoint.DataTextField = "Text"
        ddlViewPoint.DataValueField = "Value"

        ddlViewPoint.DataSource = MyObj.GetCodeMappingData("Product", "ViewPoint")
        ddlViewPoint.DataBind()
        ddlViewPoint.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub
#End Region

#Region "屬性"
    Protected Function BooleanConverter(ByVal strTMP As Object) As Boolean
        Return (IIf(strTMP.ToString() = "1", True, False))
    End Function
    Public Property ViewPoint() As String
        Get
            Return ViewState("ViewPoint")
        End Get
        Set(ByVal value As String)
            ViewState("ViewPoint") = value
        End Set
    End Property
    Public ReadOnly Property DataDT() As Data.DataTable
        Get
            Return Me.ProductHierarchyControl1.DataDT
        End Get
    End Property
    Public Property Division() As String
        Get
            Return ViewState("Division")
        End Get
        Set(ByVal value As String)
            ViewState("Division") = value
        End Set
    End Property
    Public Property RoleID() As Integer
        Get
            Return ViewState("RoleID")
        End Get
        Set(ByVal value As Integer)
            ViewState("RoleID") = value
        End Set
    End Property
    Public Property Hierarchy() As String
        Get
            Return ViewState("Hierarchy")
        End Get
        Set(ByVal value As String)
            ViewState("Hierarchy") = value
        End Set
    End Property

    Public ReadOnly Property sqlArray() As String(,)
        Get
            Return ProductHierarchyControl1.sqlArray
        End Get
    End Property
#End Region

End Class
